/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.activity.editpart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.uml2.uml.Action;
import org.eclipse.uml2.uml.CallBehaviorAction;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ElementImport;
import org.eclipse.uml2.uml.NamedElement;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.figures.IconFigure;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.activity.figure.ActionFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Controller of a CallBehaviorAction
 *
 * @author Gilles Cannenterre
 * @author Patrick Tessier
 * @since 29 mars 07
 */
public class CallBehaviorActionEditPart extends CallActionEditPart{

	public CallBehaviorActionEditPart(GraphNode graphNode,
			ModelManager modelManager) {
		super(graphNode, modelManager);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.GraphNodeWithSemanticEditPart#getUmlElement()
	 */
	@Override
	public CallBehaviorAction getUmlElement() {
		Element e = ((Uml1SemanticModelBridge)getGraphNode().getSemanticModel()).getElement();
		// Case of element import
		if(e instanceof ElementImport){
			return (CallBehaviorAction)((ElementImport) e).getImportedElement();
		}
		// This is a simple CallBehaviorAction
		return (CallBehaviorAction)e;
	}

	
	protected void refreshVisuals() {
		Debug.debug(this, "refreshVisuals()", channel);

		Dimension size = getGraphNode().getSize();
		Point p = getGraphNode().getPosition();
		Rectangle r = new Rectangle(p, size);

		IFigure fig = getFigure();

		// Classifier
		if (fig instanceof IconFigure) {
			IconFigure icf = (IconFigure) getFigure();
			icf.setName(((NamedElement) getUmlElement()).getName());
		} else {
			Action currentAction = getUmlElement();
			com.cea.papyrus.umlutils.Element currentElement = new com.cea.papyrus.umlutils.Element(getUmlElement());
			((ActionFigure) fig).refreshColors(getGraphNode());
			((ActionFigure) fig).refreshPresentation(getPresentation(),currentAction, getGraphNode());

			boolean hasIcon = currentElement.hasIcon();
			boolean hasShape = currentElement.hasShape();

			((ActionFigure) fig).refreshStereotypes(((ActionFigure) fig).stereotypesLabel(this), getPresentation(), hasIcon, hasShape);
			((ActionFigure) fig).setName(currentAction.getName());
			if(getUmlElement().getBehavior()!=null){
				String display= getUmlElement().getBehavior().getName();
				((ActionFigure) fig).setName("Call " +display);

		}
		// qualifiedName
		GraphNode gp= (GraphNode) getGraphNode().getContainer();
		((ActionFigure) fig).refreshQualifiedName(gp, getGraphNode(), getUmlElement());
	}  

	((GraphicalEditPart) getParent()).setLayoutConstraint(this, fig, r);
}
}
